# _*_ coding:utf-8 _*_

"""
# @Time　 : 2021/12/28 21:52
# @Author : king
# @File　 : after_handle_utils.py
# @project: ApiPytestFramework
# @QQ     : 772084279
# @Email  : 772084279@qq.com
# @blog   : https://ceshizhilu.blog.csdn.net/
"""
import os

from requests import Response

from outputs.Logs.log_handle import get_log
from common_utils.data_handle import json_extractor, re_extract
from common_utils.global_vars import GLOBAL_VARS

logging = get_log(os.path.split(__file__)[-1])


def after_extract(response: Response, exp: str) -> None:
    """
    :param response: request 响应对象
    :param exp: 需要提取的参数字典 '{"k1": "$.data"}' 或 '{"k1": "data:(.*?)$"}'
    :return:
    """
    if exp:
        if response_type(response) == "json":
            res = response.json()
            for k, v in exp.items():
                GLOBAL_VARS[k] = json_extractor(res, v)
        else:
            res = response.text
            for k, v in exp.items():
                GLOBAL_VARS[k] = re_extract(res, v)


def response_type(response: Response) -> str:
    """
    :param response: requests 返回
    :return: 返回响应数据类型 json或者str
    """
    try:
        response.json()
        return "json"
    except:
        return "str"
